<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
<!-- 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['jiuzai:cangkumingxi:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['jiuzai:cangkumingxi:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['jiuzai:cangkumingxi:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['jiuzai:cangkumingxi:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="cangkumingxiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="mingId" />
      <el-table-column label="物资序号" align="center" prop="wuziId" />
      <el-table-column label="入库序号" align="center" prop="rukuId" />
      <el-table-column label="出库序号" align="center" prop="cId" />
      <el-table-column label="供应商" align="center" prop="wuziShangjia" />
      <el-table-column label="生产日期" align="center" prop="mingRiqi" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.mingRiqi, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过保日期" align="center" prop="mingGuoriqi" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.mingGuoriqi, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购置日期" align="center" prop="mingMairiqi" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.mingMairiqi, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="mingDanjia" />
      <el-table-column label="是否可回收" align="center" prop="mingHui">
        <template #default="scope">
          <el-tag v-if="scope.row.mingHui === 1" type="success">可回收</el-tag>
          <el-tag v-else type="danger">不可回收</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="mingShuliang" />
      <el-table-column label="物资名称" align="center" prop="mingName" />
      <el-table-column label="物资规格" align="center" prop="mingGuige" />
      <el-table-column label="状态(0.入库1.出库)" align="center" prop="mingZhuangtai">
        <template #default="scope">
          <el-tag v-if="scope.row.mingZhuangtai === 0">入库</el-tag>
          <el-tag v-else>出库</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['jiuzai:cangkumingxi:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['jiuzai:cangkumingxi:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改物资出入库明细对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="cangkumingxiRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物资序号" prop="wuziId">
          <el-input v-model="form.wuziId" placeholder="请输入物资序号" />
        </el-form-item>
        <el-form-item label="入库序号" prop="rukuId">
          <el-input v-model="form.rukuId" placeholder="请输入入库序号" />
        </el-form-item>
        <el-form-item label="出库序号" prop="cId">
          <el-input v-model="form.cId" placeholder="请输入出库序号" />
        </el-form-item>
        <el-form-item label="供应商" prop="wuziShangjia">
          <el-input v-model="form.wuziShangjia" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="生产日期" prop="mingRiqi">
          <el-date-picker clearable v-model="form.mingRiqi" type="date" value-format="YYYY-MM-DD" placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="过保日期" prop="mingGuoriqi">
          <el-date-picker clearable v-model="form.mingGuoriqi" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择过保日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购置日期" prop="mingMairiqi">
          <el-date-picker clearable v-model="form.mingMairiqi" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择购置日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单价" prop="mingDanjia">
          <el-input v-model="form.mingDanjia" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="是否可回收" prop="mingHui">
          <el-input v-model="form.mingHui" placeholder="请输入是否可回收" />
        </el-form-item>
        <el-form-item label="数量" prop="mingShuliang">
          <el-input v-model="form.mingShuliang" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="物资名称" prop="mingName">
          <el-input v-model="form.mingName" placeholder="请输入物资名称" />
        </el-form-item>
        <el-form-item label="物资规格" prop="mingGuige">
          <el-input v-model="form.mingGuige" placeholder="请输入物资规格" />
        </el-form-item>
        <el-form-item label="状态(0.入库1.出库)" prop="mingZhuangtai">
          <el-input v-model="form.mingZhuangtai" placeholder="请输入状态(0.入库1.出库)" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cangkumingxi">
import { listCangkumingxi, getCangkumingxi, delCangkumingxi, addCangkumingxi, updateCangkumingxi } from "@/api/jiuzai/cangkumingxi"

const { proxy } = getCurrentInstance()

const cangkumingxiList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询物资出入库明细列表 */
function getList() {
  loading.value = true
  listCangkumingxi(queryParams.value).then(response => {
    cangkumingxiList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    mingId: null,
    wuziId: null,
    rukuId: null,
    cId: null,
    wuziShangjia: null,
    mingRiqi: null,
    mingGuoriqi: null,
    mingMairiqi: null,
    mingDanjia: null,
    mingHui: null,
    mingShuliang: null,
    mingName: null,
    mingGuige: null,
    mingZhuangtai: null
  }
  proxy.resetForm("cangkumingxiRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.mingId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加物资出入库明细"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _mingId = row.mingId || ids.value
  getCangkumingxi(_mingId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改物资出入库明细"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["cangkumingxiRef"].validate(valid => {
    if (valid) {
      if (form.value.mingId != null) {
        updateCangkumingxi(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCangkumingxi(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _mingIds = row.mingId || ids.value
  proxy.$modal.confirm('是否确认删除物资出入库明细编号为"' + _mingIds + '"的数据项？').then(function () {
    return delCangkumingxi(_mingIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('jiuzai/cangkumingxi/export', {
    ...queryParams.value
  }, `cangkumingxi_${new Date().getTime()}.xlsx`)
}

getList()
</script>
